{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2a11135a-cb9d-49eb-964d-2607e82e6955",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 10.31, 'python') <class 'tuple'>\n",
      "(1, 10.31, 'python') <class 'tuple'>\n",
      "2\n",
      "(6, 7, 8)\n",
      "(1, 2, 3, 4, 5)\n",
      "(1, 2, 3, 4, 5, 6, 7, 8)\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "元组\n",
    "「元组」定义语法为：(元素1, 元素2, ..., 元素n)\n",
    "\n",
    "小括号把所有元素绑在一起\n",
    "逗号将每个元素一一分开\n",
    "1. 创建和访问一个元组\n",
    "Python 的元组与列表类似，不同之处在于tuple被创建后就不能对其进行修改，类似字符串。\n",
    "元组使用小括号，列表使用方括号。\n",
    "元组与列表类似，也用整数来对它进行索引 (indexing) 和切片 (slicing)。\n",
    "'''\n",
    "t1 = (1, 10.31, 'python')\n",
    "t2 = 1, 10.31, 'python'\n",
    "print(t1, type(t1))\n",
    "# (1, 10.31, 'python') <class 'tuple'>\n",
    "\n",
    "print(t2, type(t2))\n",
    "# (1, 10.31, 'python') <class 'tuple'>\n",
    "\n",
    "tuple1 = (1, 2, 3, 4, 5, 6, 7, 8)\n",
    "print(tuple1[1])  # 2\n",
    "print(tuple1[5:])  # (6, 7, 8)\n",
    "print(tuple1[:5])  # (1, 2, 3, 4, 5)\n",
    "tuple2 = tuple1[:]\n",
    "print(tuple2)  # (1, 2, 3, 4, 5, 6, 7, 8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a1f20fe4-022c-47fb-b819-06d3b3144e54",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'>\n",
      "<class 'tuple'>\n",
      "<class 'list'>\n",
      "<class 'tuple'>\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "创建元组可以用小括号 ()，也可以什么都不用，为了可读性，建议还是用 ()。\n",
    "元组中只包含一个元素时，需要在元素后面添加逗号，否则括号会被当作运算符使用。\n",
    "'''\n",
    "x = (1)\n",
    "print(type(x))  # <class 'int'>\n",
    "x = 2, 3, 4, 5\n",
    "print(type(x))  # <class 'tuple'>\n",
    "x = []\n",
    "print(type(x))  # <class 'list'>\n",
    "x = ()\n",
    "print(type(x))  # <class 'tuple'>\n",
    "x = (1,)\n",
    "print(type(x))  # <class 'tuple'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "db514de0-16af-4b4d-aab8-00f5ca14301d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "64\n",
      "(8, 8, 8, 8, 8, 8, 8, 8)\n"
     ]
    }
   ],
   "source": [
    "print(8 * (8))  # 64\n",
    "print(8 * (8,))  # (8, 8, 8, 8, 8, 8, 8, 8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "206b949d-a998-4d22-a161-227bd9c4bd6c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "((1, 10.31, 'python'), ('data', 11))\n",
      "(1, 10.31, 'python')\n",
      "1 10.31 python\n",
      "(1, 10.31)\n"
     ]
    }
   ],
   "source": [
    "x = (1, 10.31, 'python'), ('data', 11)\n",
    "print(x)\n",
    "# ((1, 10.31, 'python'), ('data', 11))\n",
    "\n",
    "print(x[0])\n",
    "# (1, 10.31, 'python')\n",
    "print(x[0][0], x[0][1], x[0][2])\n",
    "# 1 10.31 python\n",
    "\n",
    "print(x[0][0:2])\n",
    "# (1, 10.31)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2409fd0d-abdd-4419-bfb3-4fee6acf242f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday')\n",
      "(1, 2, 3, [4, 5, 6])\n",
      "(1, 2, 3, [9, 5, 6])\n"
     ]
    }
   ],
   "source": [
    "# 更新和删除一个元组\n",
    "week = ('Monday', 'Tuesday', 'Thursday', 'Friday')\n",
    "week = week[:2] + ('Wednesday',) + week[2:]\n",
    "print(week)  # ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday')\n",
    "\n",
    "t1 = (1, 2, 3, [4, 5, 6])\n",
    "print(t1)  # (1, 2, 3, [4, 5, 6])\n",
    "\n",
    "t1[3][0] = 9\n",
    "print(t1)  # (1, 2, 3, [9, 5, 6])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "33e8dee9-8317-42b1-bb46-acb89b76b839",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n",
      "(123, 456, 456, 123)\n",
      "(123, 456, 123, 456, 123, 456)\n",
      "(123, 456, 123, 456, 123, 456)\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "等号操作符：==\n",
    "连接操作符 +\n",
    "重复操作符 *\n",
    "成员关系操作符 in、not in\n",
    "「等号 ==」，只有成员、成员位置都相同时才返回True。\n",
    "\n",
    "元组拼接有两种方式，用「加号 +」和「乘号 *」，前者首尾拼接，后者复制拼接。\n",
    "'''\n",
    "\n",
    "t1 = (123, 456)\n",
    "t2 = (456, 123)\n",
    "t3 = (123, 456)\n",
    "\n",
    "print(t1 == t2)  # False\n",
    "print(t1 == t3)  # True\n",
    "\n",
    "t4 = t1 + t2\n",
    "print(t4)  # (123, 456, 456, 123)\n",
    "\n",
    "t5 = t3 * 3\n",
    "print(t5)  # (123, 456, 123, 456, 123, 456)\n",
    "\n",
    "t3 *= 3\n",
    "print(t3)  # (123, 456, 123, 456, 123, 456)\n",
    "\n",
    "print(123 in t3)  # True\n",
    "print(456 not in t3)  # False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "869336db-0f46-4407-9ed3-61b64d8f45b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "元组大小和内容都不可更改，因此只有 count 和 index 两种方法。\n",
    "print(t.count('python'))  # 1\n",
    "print(t.index(10.31))  # 1\n",
    "'''\n",
    "t = (1, 10.31, 'python')\n",
    "print(t.count('python'))  # 1\n",
    "print(t.index(10.31))  # 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9eff243c-2448-4900-823b-4d6d1fc9a807",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 10.31 python\n"
     ]
    }
   ],
   "source": [
    "# 解压（unpack）一维元组（有几个元素左边括号定义几个变量）\n",
    "t = (1, 10.31, 'python')\n",
    "(a, b, c) = t\n",
    "print(a, b, c)\n",
    "# 1 10.31 python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "825acc71-5dfe-4908-bf0a-15cafab0ad1b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 10.31 OK python\n"
     ]
    }
   ],
   "source": [
    "# 解压二维元组（按照元组里的元组结构来定义变量）\n",
    "t = (1, 10.31, ('OK', 'python'))\n",
    "(a, b, (c, d)) = t\n",
    "print(a, b, c, d)\n",
    "# 1 10.31 OK python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9cf3b7e1-caa0-46b1-9c3c-12842bf009cb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 5\n",
      "[3, 4]\n"
     ]
    }
   ],
   "source": [
    "# 如果你只想要元组其中几个元素，用通配符「*」，英文叫 wildcard，在计算机语言中代表一个或多个元素。下例就是把多个元素丢给了 rest 变量。\n",
    "t = 1, 2, 3, 4, 5\n",
    "a, b, *rest, c = t\n",
    "print(a, b, c)  # 1 2 5\n",
    "print(rest)  # [3, 4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "036f5517-d09c-4479-aef5-5f3447cf659e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 5\n"
     ]
    }
   ],
   "source": [
    "#如果你根本不在乎 rest 变量，那么就用通配符「*」加上下划线「_」。\n",
    "t = 1, 2, 3, 4, 5\n",
    "a, b, *_, c = t\n",
    "print(a, b, c)  # 1 2 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "69e9f0ef-b15f-498d-b287-3c57ea2ac63d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.23"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
